<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <!-- 搜索门诊卡状态-->
      <el-form-item label="门诊卡状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态 " clearable size="small">
          <el-option v-for="dict in dict.type.sys_outpatient_cardstatus" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <!-- -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['outpatient:info:add']">办卡</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleguahao"
          v-hasPermi="['outpatient:info:edit']">挂号</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['outpatient:info:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['outpatient:info:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['outpatient:info:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="门诊卡ID" align="center" prop="id" />
      <el-table-column label="金额" align="center" prop="money" />
      <el-table-column label="启用日期" align="center" prop="enableTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.enableTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="挂失日期" align="center" prop="lossTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lossTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="卡作废日期" align="center" prop="deadTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deadTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退卡日期" align="center" prop="returnTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="门诊卡状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_outpatient_cardstatus" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="押金" align="center" prop="deposit" />
      <el-table-column label="个人档案ID" align="center" prop="personid" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div v-if="scope.row.status == 1">
            <el-button  size="mini" type="text" icon="el-icon-edit" @click="handlegs(scope.row)"
              v-hasPermi="['outpatient:info:edit']">挂失</el-button>
            <el-button  size="mini" type="text" icon="el-icon-edit" @click="handlezf(scope.row)"
              v-hasPermi="['outpatient:info:edit']">作废</el-button>
            <el-button  size="mini" type="text" icon="el-icon-edit" @click="handletk(scope.row)"
              v-hasPermi="['outpatient:info:edit']">退卡</el-button>
            <el-button  size="mini" type="text" icon="el-icon-edit" @click="handlechongzhi(scope.row)"
              v-hasPermi="['outpatient:info:edit']">充取现</el-button>
          </div>
          <div v-if="scope.row.status == 2">
            <el-button  size="mini" type="text" icon="el-icon-edit" @click="handleqiyong(scope.row)"
              >启用</el-button>
            <el-button  size="mini" type="text" icon="el-icon-edit" @click="handlezf(scope.row)"
              v-hasPermi="['outpatient:info:edit']">作废</el-button>
          </div>
          <div v-if="scope.row.status == 3">
            <el-button  size="mini" type="text" icon="el-icon-edit" @click="handleqiyong(scope.row)"
              >启用</el-button>
          </div>
          <div v-if="scope.row.status == 4">

          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改门诊卡信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="个人档案id" prop="personid">
          <el-input v-model="form.personid" placeholder="请输入个人档案id与个人档案表是多对一的关系" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- ------------------------------挂号 -------------------------------------------- -->
    <el-dialog :title="title" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="个人档案id" prop="personid">
          <el-input v-model="form.personid" placeholder="请输入个人档案id" />
        </el-form-item>
        <el-form-item label="门诊卡id" prop="id">
          <el-input v-model="form.id" placeholder="请输入门诊卡id" />
        </el-form-item>
        <el-row :gutter="20">
          <!--部门数据-->
          <el-col :span="8" :xs="24">
            <div class="head-container">
              <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false"
                :filter-node-method="filterNode" ref="tree" default-expand-all @node-click="handleNodeClick" />
            </div>
          </el-col>
          <el-col :span="16" :xs="24">
            <el-table v-loading="loading" ref="userTable" :data="userList" @current-change="handleCurrentChange">
              <el-table-column type="selection"  align="center" />
              <el-table-column label="用户编号" align="center" key="userId" prop="userId"  />
              <el-table-column label="用户名称" align="center" key="userName" prop="userName" :show-overflow-tooltip="true" />
              <el-table-column label="用户昵称" align="center" key="nickName" prop="nickName" :show-overflow-tooltip="true" />
            </el-table>
          </el-col>
        </el-row>
        <el-form-item label="医生id" prop="doctorid">
          <el-input v-model="form.doctorid" placeholder="请输入医生id" />
        </el-form-item>
        <el-form-item label="挂号费用" prop="pirce">
          <el-input v-model="form.pirce" placeholder="请输入挂号费用" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- ------------------------------------充值或提现----------------------------------------- -->
    <el-dialog :title="title" :visible.sync="open2" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="门诊卡id" prop="outpatientid">
          <el-input v-model="form.outpatientid" placeholder="请输入门诊卡id" />
        </el-form-item> -->
        <el-form-item label="金额" prop="money1">
          <el-input v-model="form.money1" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="请选择操作" prop="type">
          <el-select v-model="form.type" placeholder="请选择">
            <el-option v-for="dict in dict.type.sys_recharge_record" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="chongzhitijiao">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- ——————————————————————————————————————确认订单————————————————————————————————————————————-->
    <el-dialog :title="title" :visible.sync="open3" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="金额" prop="money1" disabled>
          <el-input v-model="form.money1" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="操作" prop="type" disabled>
          <el-select v-model="form.type" placeholder="请选择1充值2取现">
            <el-option v-for="dict in dict.type.sys_recharge_record" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
          <!-- <el-input v-model="form.type"  /> -->
        </el-form-item>
        <el-form-item label="门诊卡id" prop="outpatientid">
          <el-input v-model="form.outpatientid" placeholder="请输入门诊卡id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="querendingdan">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>





  </div>
</template>

<script>
  import {
    listInfo,
    getInfo,
    delInfo,
    addInfo,
    updateInfo
  } from "@/api/outpatient/info";

/* ---------------------------------部门-------------------------- */
  import {
    listUser,
    getUser,
    delUser,
    addUser,
    updateUser,
    resetUserPwd,
    changeUserStatus
  } from "@/api/system/user";
  import {
    getToken
  } from "@/utils/auth";
  import {
    treeselect
  } from "@/api/system/dept";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

/* -----------------------------------挂号------------------------ */
  import {
    listRegister,
    getRegister,
    delRegister,
    addRegister,
    updateRegister
  } from "@/api/cost/register";

  /* ***********************************************************门诊充值信息************************************* */
  import {
    listRecord,
    getRecord,
    delRecord,
    addRecord,
    updateRecord
  } from "@/api/outpatient/record";


  export default {
    name: "Info",
    dicts: ['sys_outpatient_cardstatus','sys_recharge_record'],
    data() {
      return {
        // 用户表格数据
        userList: null,
        currentRow: null,
        // 部门树选项
        deptOptions: undefined,
        defaultProps: {
          children: "children",
          label: "label"
        },
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 门诊卡信息表格数据
        infoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //挂号是否弹出框
        open1: false,
        //费用是否弹出框
        open2: false,
        //确认订单是否弹出框
        open3: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          money: null,
          enableTime: null,
          lossTime: null,
          deadTime: null,
          returnTime: null,
          status: null,
          deposit: null,
          personid: null,
          doctorid: null
        },
        querParams1: {
          personid:null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getTreeselect();
    },
    methods: {
      /** 查询门诊卡信息列表 */
      getList() {
        this.loading = true;
        listInfo(this.queryParams).then(response => {
          this.infoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 查询部门下拉树结构 */
       getTreeselect() {
         treeselect().then(response => {
           this.deptOptions = response.data;
         });
       },
       //节点按钮
       handleNodeClick(data) {
         this.queryParams.deptId = data.id;
         this.handleUserQuery();
       },
       //
       handleUserQuery() {
         this.queryParams.pageNum = 1;
         this.getUserList();
       },
       /** 查询用户列表 */
       getUserList() {
         this.loading = true;
         listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
           this.userList = response.rows;
           this.total = response.total;
           this.loading = false;
         });
       },
       //
       handleCurrentChange(val) {
         if(val!=null){
         console.log(val.userId)
         this.form.doctorid = val.userId;
         }
       },
       setCurrent(row){
         this.$refs.userTable.setCurrentRow(row);
       },
      // 筛选节点
      filterNode(value, data) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1;
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          money: null,
          money1: null,
          enableTime: null,
          lossTime: null,
          deadTime: null,
          returnTime: null,
          status: 0,
          deposit: null,
          personid: null,
          outpatientid: null,
          type: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加门诊卡信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getInfo(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改门诊卡信息";
        });
      },
      /* 启用按钮*/
      handleqiyong(row) {
        this.reset();
        const id = row.id || this.ids
        getInfo(id).then(response => {
          response.data.status=1;
          response.data.enableTime=new Date
          this.form = response.data;
          updateInfo(this.form).then(response =>{
            this.$modal.msgSuccess("启用成功");
            this.getList()
          })
        });
      },
      /* 挂失按钮*/
      handlegs(row) {
        this.reset();
        const id = row.id || this.ids
        getInfo(id).then(response => {
          response.data.status=2;
          response.data.lossTime=new Date
          this.form = response.data;
          updateInfo(this.form).then(response =>{
            this.$modal.msgSuccess("挂失成功");
            this.getList()
          })
        });
      },
      /* 作废按钮*/
      handlezf(row) {
        this.reset();
        const id = row.id || this.ids
        getInfo(id).then(response => {
          response.data.status=3;
          response.data.deadTime=new Date
          this.form = response.data;
          updateInfo(this.form).then(response =>{
            this.$modal.msgSuccess("作废成功");
            this.getList()
          })
        });
      },
      /* 退卡按钮*/
      handletk(row) {
        this.reset();
        const id = row.id || this.ids
        getInfo(id).then(response => {
          response.data.status=4;
          response.data.returnTime=new Date
          this.form = response.data;
          console.log(this.form.money)
          if(this.form.money!=0){
            this.$modal.msgWarning("退卡操作前需取现余额为零元");
          }else{
          updateInfo(this.form).then(response =>{
            this.$modal.msgSuccess("退卡成功");
            this.getList()
          })
          }
        });
      },
      /* ****************************************************** 挂号 ************************************************/
      /** 挂号按钮操作 */
      handleguahao(row) {
        this.reset();
        const id = row.id || this.ids
        getInfo(id).then(response => {
          if(response.data.status==1){
              this.form=response.data
              this.querParams1=this.form
              listRegister(this.querParams1).then(r => {
                console.log(r.rows)
                if(r.rows.length==0){
                  console.log("-------"+r.rows)
                  response.data.outpatientid=response.data.id
                  this.form = response.data;
                  this.open1 = true;
                  this.title = "挂号";
              }else{
                  console.log("-------______"+r.rows)
                  console.log(r.rows)
                  this.$modal.msgWarning("已经在就诊或排队中");
                for(let i=0;i < r.rows.length-1;i++){
                  if(r.rows.type==3){
                      response.data.outpatientid=response.data.id
                      this.form = response.data;
                      this.open1 = true;
                      this.title = "挂号";
                      // return
                  }else{
                    this.$modal.msgWarning("已经在就诊或排队中");
                  }
                }
              }
            });
          }else{
            this.$modal.msgWarning("请选择已启用的，在进行挂号");
          }
        });
      },
      /* ********************************************************费用************************************************  */
      /** 充值按钮操作 */
      handlechongzhi(row) {
        this.reset();
        const id = row.id || this.ids
        getInfo(id).then(response => {
          response.data.outpatientid=response.data.id
          console.log(response.data.money)
          this.form = response.data;
          this.open2 = true;
          this.title = "充值信息";
        });
      },
      /* 充值提交按钮*/
      chongzhitijiao(){
        this.$refs["form"].validate(valid=> {
          if(valid){
            if(this.form.type == 1){
            this.form.money = parseFloat(this.form.money)+parseFloat(this.form.money1)

              updateInfo(this.form).then(response => {
                this.$modal.msgSuccess("充值成功");
                /* console.log(response.data.money); */
                this.open2 = false;
                this.open3 = true;
                this.title = "确认订单信息";
              });
            }else if(this.form.type == 2){
                if(parseFloat(this.form.money)<parseFloat(this.form.money1)){

                  this.$modal.msgWarning("怕是日子过懵了");
                }else{
                  this.form.money = parseFloat(this.form.money)-parseFloat(this.form.money1)
                updateInfo(this.form).then(response => {
                  this.$modal.msgSuccess("取现成功");
                  this.open2 = false;
                  this.open3 = true;
                  this.title = "确认订单信息";
                });
              }
            }else {
              this.$modal.msgWarning("请选择操作");
              }
            }
          });
      },

      /* 订单提交*/
      querendingdan() {
        this.$refs["form"].validate(valid => {
          if(valid){

            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("订单提交成功");
              this.open3 = false;
              this.getList();
            });
          }
        })
      },

      submitForm1() {
        this.$refs["form"].validate(valid => {
          if (valid) {
                  addRegister(this.form).then(response => {
                    this.$modal.msgSuccess("挂号成功");
                    this.open1 = false;
                    this.getList();
                  });
          }
        });
      },




      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null & this.form.personid == null) {
              updateInfo(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addInfo(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除门诊卡信息编号为"' + ids + '"的数据项？').then(function() {
          return delInfo(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('outpatient/info/export', {
          ...this.queryParams
        }, `info_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
